package cn.openea.perpartner.payment.web.points.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.utils.Arith;
import cn.openea.perpartner.common.redis.service.RedisCache;
import cn.openea.perpartner.payment.web.points.dto.GrowthDto;
import cn.openea.perpartner.payment.web.points.entity.GrowthLog;
import cn.openea.perpartner.payment.web.points.entity.Points;
import cn.openea.perpartner.payment.web.points.mapper.GrowthLogMapper;
import cn.openea.perpartner.payment.web.points.mapper.PointsMapper;
import cn.openea.perpartner.payment.web.points.service.IGrowthService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-12-14 19:44
 */
@Slf4j
@Service
public class GrowthServiceImpl implements IGrowthService {
    @Resource
    private PointsMapper pointsMapper;
    @Resource
    private GrowthLogMapper growthLogMapper;
    @Resource
    private RedisCache redisCache;

    @Value("${growth.redis.expire}")
    private long expireTime;

    @Override
    public boolean save(GrowthDto growthDto) {
        LambdaQueryWrapper<Points> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Points::getUserId, growthDto.getUserId());
        Points points = pointsMapper.selectOne(wrapper);
        if (BeanUtil.isNotEmpty(points)) {
            double growth = Arith.add(Convert.toDouble(points.getGrowth()), Convert.toDouble(growthDto.getGrowth()));
            points.setGrowth(Convert.toStr(growth));
            pointsMapper.updateById(points);
        } else {
            points = new Points();
            points.setUserId(growthDto.getUserId());
            points.setGrowth(growthDto.getGrowth());
            pointsMapper.insert(points);
        }
        String key = getCacheKey(growthDto.getUserId());
        redisCache.setCacheObject(key, points.getGrowth());
        redisCache.expire(key, expireTime, TimeUnit.MINUTES);
        GrowthLog growthLog = new GrowthLog();
        BeanUtil.copyProperties(growthDto, growthLog);
        return growthLogMapper.insert(growthLog)>0;
    }

    @Override
    public String getByUserId(String userId) {
        String growth = Convert.toStr(redisCache.getCacheObject(userId.concat(".growth")));
        if (StrUtil.isNotBlank(growth)) {
            return growth;
        }
        LambdaQueryWrapper<Points> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Points::getUserId, userId);
        Points points = pointsMapper.selectOne(wrapper);
        if (BeanUtil.isNotEmpty(points)) {
            growth = points.getGrowth();
            redisCache.setCacheObject(getCacheKey(userId), growth);
            redisCache.expire(getCacheKey(userId), expireTime, TimeUnit.MINUTES);
            return growth;
        }
        return "0";
    }

    private String getCacheKey(String userId) {
        return "payment:".concat("growth.").concat(userId);
    }
}
